package gov.nhso.prov.acc.servlets;

import gov.nhso.prov.acc.apps.acc67.AccUpload;
import gov.nhso.prov.acc.core.ConfigBundle;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import javazoom.upload.MultipartFormDataRequest;
/**
 * Servlet implementation class for Servlet: ServletDispatcher
 *
 */
 public class ServletDispatcher extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
	private static final long serialVersionUID = -7543861616375197631L;
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public ServletDispatcher() {
		super();
	}
	
	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.moduleFilter(request, response);
	}  	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.moduleFilter(request, response);
	}
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#moduleFilter(HttpServletRequest request, HttpServletResponse response)
	 */
 	@SuppressWarnings("rawtypes")
	protected void moduleFilter(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			if (request.getParameter("app") == null) {
				if (MultipartFormDataRequest.isMultipartFormData(request)) {
					MultipartFormDataRequest mrequest = new MultipartFormDataRequest(request);
					AccUpload upload = new AccUpload(request, mrequest, response);
					this.dispatcherForward(request, response, upload.url);
				} else {
					HttpSession session = request.getSession();
					Enumeration enume =	session.getAttributeNames();
				 	while (enume.hasMoreElements()) {
				 		String elem = (String) enume.nextElement();
				 		session.removeAttribute(elem);
				 	}
				 	request.setAttribute("version", ConfigBundle.getValue("VERSION"));
				 	request.setAttribute("deployDate", ConfigBundle.getValue("DEPLOY_DATE"));
				 	request.setAttribute("deployTime", ConfigBundle.getValue("DEPLOY_TIME"));
					this.dispatcherForward(request, response, "/index.jsp");
				}
			} else if (!request.getParameter("app").equals("ACC670000") && request.getSession().getAttribute("userInfo") == null) {
			 	request.setAttribute("version", ConfigBundle.getValue("VERSION"));
			 	request.setAttribute("deployDate", ConfigBundle.getValue("DEPLOY_DATE"));
			 	request.setAttribute("deployTime", ConfigBundle.getValue("DEPLOY_TIME"));
			 	this.dispatcherForward(request, response, "/index.jsp");
		    } else {
		    	this.doProcess(request, response);
		    }
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doProcess(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			//TODO after child process 
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * dispatcherForward
	 * @param dispatcherURL
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void dispatcherForward(HttpServletRequest request, HttpServletResponse response, String dispatcherURL) throws ServletException, IOException {
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(dispatcherURL);
		dispatcher.forward(request, response);
	}
	
	protected String encodeUTF(String str) throws Exception {
        String output = "";
        if (str != null) {
            try {
                output = new String(str.getBytes("ISO8859_1"), "UTF-8");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return output;
    }

 	@SuppressWarnings("rawtypes")
	protected void setAllRequest(HttpServletRequest request) throws Exception {
		Enumeration enumer = request.getAttributeNames();
		while (enumer.hasMoreElements()) {
			String name = (String) enumer.nextElement();
			request.setAttribute(name, request.getAttribute(name));
		}
		enumer = request.getParameterNames();
		while (enumer.hasMoreElements()) {
			String name = (String) enumer.nextElement();
			request.setAttribute(name, this.encodeUTF(request.getParameter(name)));
		}
	}
}